<?php

defined( 'IN_CLASS' ) or die( 'Access Error!' );

class piccontrol extends control {
	
	public function __construct() {
		$aC = $this->_chechlogin(); //检查用户登陆
		all::$aU = model::member()->get( $aC['id'] );
	}
	
	public function onindex() { //显示相片页面
		all::$aOut['sms_num'] = model::sms()->checknew( all::$aU['id'] );
		all::$aOut['mod_name'] = all::$cfg['navname']['pic']; //模块名称
		all::$aOut['top10'] = model::member()->gettop10();//输出班级会员积分 TOP 10
		
		all::$aOut['album_brief'] = all::$cfg['class']['album_brief']; //相册宣言
		all::$aOut['max_size'] = all::$cfg['class']['pix_max_size']; //相册宣言
		
		all::$aOut['page_curr'] = func::input( 'page', 'i' );  //申明当前页的变量，在分页函数中将给此变量赋值，方便处理后的重定向
		all::$aOut['pictop3'] = model::pic()->getTopX(); //图片TOP3
		
		$limit = show_page( all::$cfg['class']['pix_per_page'], 'limit' );  //设定当前页中的SQL的限制
		all::$aOut['pics'] = model::pic()->ls( $limit );
		all::$aOut['page_tip'] = show_page( all::$cfg['class']['pix_per_page'] );   //得到分页结果
		
		all::$aOut['aUsed'] = model::pic()->getused();
		all::$aOut['aUsed']['total'] = all::$cfg['class']['album_volume'];
		all::$aOut['aUsed']['userate'] = round( ( all::$aOut['aUsed']['used'] / all::$aOut['aUsed']['total'] ) * 100 );//所用百分比
		
		func::display( all::$aOut, 'album.htm' );
	}
	
	public function onadd() { //添加相片
		$brief = func::input ( 'brief' ); //图片说明

		$error  = '';
		$error .= ( '' == $_FILES['file']['name'] ) ? '请选择文件' : '';
		$error .= ( $brief == '' ) ? '请写图片说明' : '';
		$error .= ( 0 < $_FILES['file']['error'] ) ? '文件上传过程出错' : '';
		$error .= !in_array( strtolower( $_FILES['file']['type'] ) , array( 'image/jpeg' , 'image/gif' , 'image/pjpeg' ) ) ? '上传的图片格式不对' : '';
		$error .= ( $_FILES['file']['size'] > all::$cfg['class']['pix_max_size'] * 1024 ) ? '上传图片超过限制大小' : '';
		
		$aUsed = model::pic()->getused();
		$error .= ( $aUsed['used'] + $_FILES['file']['size'] > all::$cfg['class']['album_volume'] * 1024 * 1024) ? '图片总量超过相册容量限制' : '';
		
		if ( $error != '' ) { func::showerr( $error ); } //提示出错信息
		model::pic()->add( all::$aU['id'], $brief ); //真正完成添加图片
		model::score()->member_score( all::$aU['id'], all::$cfg['suser']['uppic'] ); //用户积分增加
		
		func::redirect( "./index.php?m=pic&page=1" );
	}

	public function ondel() { //删除相片
		if ( $_REQUEST['pid'] ) {
			if ( is_array( $_REQUEST['pid'] ) ) {
				foreach ( $_REQUEST['pid'] as $p ) {
					$p = func::input( $p, 'i', '', true );
					$this->_delone( $p );
				}
			} else {
				$pid = func::input ( 'pid', 'i' );
				$this->_delone( $pid );
			}
		}
		
		$page_curr = func::input( 'page', 'i' );
		func::redirect( "./index.php?m=pic&page=$page_curr" );
	}
	
	public function _delone( $pid ) { //删除一张相片
		$re = model::pic()->get( $pid );
		if( all::$aU['power'] > 1 || all::$aU['id'] == $re['owner'] ) {
			return model::pic()->del( $pid, $re['filename'], $re['type'] );
		}
		return 0;
	}
	
	public function onview() { //查看某个相片
		all::$aOut['sms_num'] = model::sms()->checknew( all::$aU['id'] );
		all::$aOut['page_curr'] = func::input( 'page', 'i' );  //申明当前页的变量

		$pid = func::input ( 'pid', 'i' );
		if( !$pid ) {
			func::showerr( '出错了, 没有指定的图片.' );
		}
		
		if ( model::score()->time_out( all::$aU['id'], $pid, all::$cfg['spic']['viewpic'][1] ) ) { //判断是否能再点击
			model::score()->pix_score( $pid, all::$cfg['spic']['viewpic'][0] ); //相片加分
			model::score()->update_viewtime( all::$aU['id'], $pid ); //记录点击最后时间
		}//对此相片的积分处理
		
		all::$aOut['aPics'] = model::pic()->getleftlist( $pid );
		all::$aOut['curr'] = all::$aOut['aPics']['curr'];
		unset(all::$aOut['aPics']['curr']);
		all::$aOut['coms'] = model::comment()->getbypid( $pid ); //输出当前照片的留言
		
		func::display( all::$aOut, 'picshow.htm' );
	}

	public function ondelcom() { //删除一条相片评论
		$pid = func::input ( 'pid', 'i' );
		$cid = func::input ( 'cid', 'i' );
		if ( $cid ) {
			$re = model::comment()->get( $cid );
			if ( ( 1 < all::$aU['power'] ) || ( all::$aU['id'] == $re['author'] ) ) {  //管理员和他自己可以删除评论
				model::comment()->del( $cid );
			}
		}
		
		func::redirect( "./index.php?m=pic&a=view&pid=$pid" );
	}

	public function onaddcom() { //添加一条相片评论
		$pid = func::input ( 'pid', 'i' );
		$content = func::input ( 'content', 's', '', false, true );
		
		$aPic = model::pic()->get( $pid );
		if ( $aPic && $content ) {
			model::comment()->add( all::$aU['id'], $pid, $content );
			model::score()->member_score( all::$aU['id'], all::$cfg['suser']['rppic'] );  //用户积分增加
			model::score()->pix_score( $pid, all::$cfg['spic']['rppic'] ); //目标相片积分上升
		}
		
		func::redirect( "./index.php?m=pic&a=view&pid=$pid" );
	}
}